package com.hudongwx.food.activity;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.support.design.widget.TabLayout;


import com.hudongwx.food.MyApplication;
import com.hudongwx.food.R;
import com.hudongwx.food.fargment.CommunityFragment;
import com.hudongwx.food.fargment.HomepageFragment;
import com.hudongwx.food.fargment.MyFragment;
import com.hudongwx.food.fargment.ShoppingFragment;

public class MainActivity extends AppCompatActivity {
    private TabLayout tableLayout;
    FragmentManager manager;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tableLayout = (TabLayout) findViewById(R.id.tabLayout_tab);
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        //默认显示第一个
        transaction.replace(R.id.frameLayout_tab, new HomepageFragment());
        transaction.commit();
        tableLayout.addTab(tableLayout.newTab().setIcon(R.drawable.home).setText("首页").setTag("home"));
        tableLayout.addTab(tableLayout.newTab().setIcon(R.drawable.shopping).setText("购物").setTag("shopping"));
        tableLayout.addTab(tableLayout.newTab().setIcon(R.drawable.community).setText("社区").setTag("community"));
        tableLayout.addTab(tableLayout.newTab().setIcon(R.drawable.my).setText("我的").setTag("my"));
        tableLayout.setTabMode(TabLayout.MODE_FIXED);//平方父视图
//        tableLayout.setTabMode(TabLayout.MODE_SCROLLABLE);//滑动显示
        tableLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                HomepageFragment homepageFragment = null;
                ShoppingFragment shoppingFragment = null;
                CommunityFragment communityFragment = null;
                MyFragment myFragment = null;
                //根据tag显示不同的fragment
                if (tab.getTag().equals("home")) {
                    if (null == homepageFragment) {
                        homepageFragment = new HomepageFragment();
                        transaction = manager.beginTransaction();
                        //需要显示的
                        transaction.replace(R.id.frameLayout_tab, homepageFragment);
                        transaction.commit();
                    }

                } else if (tab.getTag().equals("shopping")) {
                    shoppingFragment = new ShoppingFragment();
                    transaction = manager.beginTransaction();
                    //需要显示的
                    transaction.replace(R.id.frameLayout_tab, shoppingFragment);
                    transaction.commit();

                } else if (tab.getTag().equals("community")) {
                    communityFragment = new CommunityFragment();
                    transaction = manager.beginTransaction();
                    //需要显示的
                    transaction.replace(R.id.frameLayout_tab, communityFragment);
                    transaction.commit();

                } else if (tab.getTag().equals("my")) {
                    myFragment = new MyFragment();
                    transaction = manager.beginTransaction();
                    //需要显示的
                    transaction.replace(R.id.frameLayout_tab, myFragment);
                    transaction.commit();

                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
